package com.liu.manager.service.impl;

import com.liu.manager.bean.ResponseResult;
import com.liu.manager.entity.SysRolerGroup;
import com.liu.manager.mapper.SysRolerGroupMapper;
import com.liu.manager.service.SysRolerGroupService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

@Service
public class SysRolerGroupSvcImpl implements SysRolerGroupService {

    @Resource
    SysRolerGroupMapper sysRolerGroupMapper;

    @Override
    public ResponseResult insertRolerGroup(SysRolerGroup sysRolerGroup) {
        if(!StringUtils.hasLength(sysRolerGroup.getGroupName())){
            return ResponseResult.ERROR("名称不能为空");
        }
        if(sysRolerGroup.getCreateTime() <= 0){
            sysRolerGroup.setCreateTime(System.currentTimeMillis());
            sysRolerGroup.setUpdateTime(System.currentTimeMillis());
        }
        sysRolerGroup.setStatus(1);
        int id = sysRolerGroupMapper.insertRolerGroup(sysRolerGroup);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(100);
        responseResult.setMsg("保存成功");
        responseResult.setResult(sysRolerGroup);
        return responseResult;
    }

    @Override
    public ResponseResult deleteRolerGroup(long id) {
        if(id <= 0){
            return ResponseResult.ERROR("id错误");
        }
        sysRolerGroupMapper.deleteRolerGroup(id);
        return ResponseResult.OK();
    }

    @Override
    public ResponseResult updateRolerGroup(SysRolerGroup sysRolerGroup) {
        if(sysRolerGroup.getId() <= 0){
            return ResponseResult.ERROR("id错误");
        }
        if(!StringUtils.hasLength(sysRolerGroup.getGroupName())){
            return ResponseResult.ERROR("角色组名称不能为空");
        }
        SysRolerGroup sysRolerGroup1 = sysRolerGroupMapper.findById(sysRolerGroup.getId());
        sysRolerGroup1.setUpdateTime(System.currentTimeMillis());
        sysRolerGroup1.setStatus(1);
        sysRolerGroup1.setGroupName(sysRolerGroup.getGroupName());
        sysRolerGroupMapper.updateRolerGroup(sysRolerGroup1);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(100);
        responseResult.setMsg("更新成功");
        responseResult.setResult(sysRolerGroup1);
        return responseResult;
    }

    @Override
    public ResponseResult findById(long id) {
        if(id <= 0){
            return ResponseResult.ERROR("id错误");
        }
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(100);
        responseResult.setMsg("查询成功");
        responseResult.setResult(sysRolerGroupMapper.findById(id));
        return responseResult;
    }

    @Override
    public ResponseResult findAll() {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(100);
        responseResult.setMsg("查询成功");
        responseResult.setResult(sysRolerGroupMapper.findALL());
        return responseResult;
    }
}
